//
//  HomeWeightView.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-19.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "HomeWeightView.h"

//最大值
#define MyMax   150

//转盘角度，其实值从最下面开始
#define Angle(size)      (M_PI/180*(size+90))

//动画时间
#define AnimationTime       3

//动画组数
#define AnimationNumber     200

@interface HomeWeightView()
{
    //
    NSTimer *timer;
    
    //总角度
    CGFloat totalAngle;
    
    //
    NSMutableArray *dataArray;
    NSInteger number;
    CGFloat start;
    
    //
}
@end

@implementation HomeWeightView

-(void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //画内圆
    CGContextSetFillColorWithColor(context, GET_COLOR(@"ff8cb7").CGColor);
    CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2, 125/2, 0, 2*M_PI, 0);    CGContextDrawPath(context, kCGPathFill);

    //画外环
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:(self.frame.size.width-14)/2 startAngle:Angle(0) endAngle:Angle(360) clockwise:YES];
    [[UIColor whiteColor] setStroke];
    path.lineWidth = 14;
    [path stroke];

    //画外环阴影
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:(self.frame.size.width-30)/2 startAngle:Angle(0) endAngle:Angle(360) clockwise:YES];
    [GET_COLOR(@"e2e2e2") setStroke];
    path1.lineWidth = 1;
    [path1 stroke];
    
    //体重
    if (totalAngle>0) {
        
        number++;
        
        //画圆圈颜色
        start = 0;
        
        NSDictionary *dicc = [dataArray objectAtIndex:number-1];
        
        UIBezierPath *backCircle = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:(self.frame.size.width-14)/2 startAngle:Angle(start) endAngle:Angle([[dicc objectForKey:@"Angle"] floatValue]) clockwise:YES];
        [GET_COLOR(@"fe8cb7") setStroke];
        backCircle.lineWidth = 5;
        [backCircle stroke];
        
        start = [[dicc objectForKey:@"Angle"] floatValue];
    }
}

-(void)setMySize:(CGFloat)size
{
    //计算所需总角度
    totalAngle = size/MyMax*360;
    
    //每次动画移动角度
    CGFloat avgAngle = totalAngle/AnimationNumber;
    
    dataArray = [[NSMutableArray alloc ]initWithCapacity:1];
    for (int i = 1; i <= AnimationNumber; i++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
        [dic setObject:[NSNumber numberWithFloat:avgAngle*i] forKey:@"Angle"];
        [dataArray addObject:dic];
    }
    
    timer = [[NSTimer scheduledTimerWithTimeInterval:AnimationTime/AnimationNumber target:self selector:@selector(AnimationStart) userInfo:nil repeats:YES] PreventionNSTimerStop];
}

-(void)AnimationStart
{
    if (number == AnimationNumber) {
        [timer invalidate];
        
        totalAngle = 0;
        [dataArray removeAllObjects];
        dataArray = nil;
        number = 0;
    }else{
        [self setNeedsDisplay];
    }
}

@end
